package www.openteam.com.adapter;

import android.content.Context;
import android.support.v7.util.SortedList;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import www.openteam.com.R;
import www.openteam.com.bean.DeliverGoods;
import www.openteam.com.bean.ProStock;
import www.openteam.com.bean.TakeDeliverGoods;

/**
 * Created by kylin on 2016/8/19.
 */
public class TDGAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "TDGAdapter";
    private LayoutInflater mLayoutInflater;
    private SortedList<TakeDeliverGoods.DataBean> mSortedList;
    private Context context;
    List<Boolean> isflag = new ArrayList<>();
    public OnLineClickListener onLineClickListener;

    public interface OnLineClickListener{
        void onClick(TakeDeliverGoods.DataBean data);
    }

    public void setOnLineClickListener(OnLineClickListener onLineClickListener) {
        this.onLineClickListener = onLineClickListener;
    }

    public TDGAdapter(Context context) {
        mLayoutInflater = LayoutInflater.from(context);
//        Log.d(TAG, "DataAdapter: 1");
        mSortedList = new SortedList<>(TakeDeliverGoods.DataBean.class, new SortedList.Callback<TakeDeliverGoods.DataBean>() {

            /**
             * 返回一个负整数（第一个参数小于第二个）、零（相等）或正整数（第一个参数大于第二个）
             */
            @Override
            public int compare(TakeDeliverGoods.DataBean o1, TakeDeliverGoods.DataBean o2) {
                if (Integer.parseInt(o1.getRownumber()) < Integer.parseInt(o2.getRownumber())) {
                    return -1;
                } else if (Integer.parseInt(o1.getRownumber()) > Integer.parseInt(o2.getRownumber())) {
                    return 1;
                }

                return 0;
            }

            @Override
            public boolean areContentsTheSame(TakeDeliverGoods.DataBean oldItem, TakeDeliverGoods.DataBean newItem) {
                return oldItem.getRownumber().equals(oldItem.getRownumber());
            }

            @Override
            public boolean areItemsTheSame(TakeDeliverGoods.DataBean item1, TakeDeliverGoods.DataBean item2) {


                return item1.getRownumber() == item2.getRownumber();
            }

            @Override
            public void onInserted(int position, int count) {
                notifyItemRangeInserted(position, count);
            }

            @Override
            public void onRemoved(int position, int count) {
                notifyItemRangeRemoved(position, count);
            }

            @Override
            public void onMoved(int fromPosition, int toPosition) {
                notifyItemMoved(fromPosition, toPosition);
            }

            @Override
            public void onChanged(int position, int count) {
                notifyItemRangeChanged(position, count);
            }
        });
    }

    public void addItems(ArrayList<TakeDeliverGoods.DataBean> list) {
        mSortedList.beginBatchedUpdates();
        for (TakeDeliverGoods.DataBean itemModel : list) {
            mSortedList.add(itemModel);
            isflag.add(false);
        }
        mSortedList.endBatchedUpdates();
    }

    public void deleteItems(ArrayList<TakeDeliverGoods.DataBean> items) {
        mSortedList.beginBatchedUpdates();
        for (TakeDeliverGoods.DataBean item : items) {
            mSortedList.remove(item);
        }
        mSortedList.endBatchedUpdates();
    }
    public void addContext(Context context){
        this.context=context;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        Log.d(TAG, "DataAdapter: 3");
        return new TDGAdapter.tdViewHolder(mLayoutInflater.inflate(R.layout.item_base_no_audit, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
//        Log.d(TAG, "DataAdapter:4");
        final TakeDeliverGoods.DataBean data = mSortedList.get(position);

        TDGAdapter.tdViewHolder viewHolder = (TDGAdapter.tdViewHolder) holder;
        if (isflag.get(position)){
            viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.green_shallow));
        }else {
            viewHolder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.blue_shallow));
        }
        viewHolder.textView.setText("编号:"+data.getRownumber()+"");
        String vName ="";
        if (!data.getVendSName().equals(null)){
            vName=data.getVendSName();
        }else{
            vName= data.getVendName();
        }
        viewHolder.tv_title.setText("采购单号:"+data.getRdsno() + "\n" +"供应商名称:"+vName);
        viewHolder.lineItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onLineClickListener!=null){
                    int size = isflag.size();
                    for (int i = 0; i < size; i++) {
                        isflag.set(i, false);
                    }
                    isflag.set(position, true);
                    notifyDataSetChanged();
                    onLineClickListener.onClick(data);
                }
            }
        });

    }


    @Override
    public int getItemCount() {

//        Log.d(TAG, "getItemCount: " +mSortedList.size());
        return mSortedList.size();
    }

    static class tdViewHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView tv_title;
        private CardView cardView;
        private LinearLayout lineItem;

        public tdViewHolder(View itemView) {
            super(itemView);
//            Log.d(TAG, "DataAdapter: 5");
            textView = (TextView) itemView.findViewById(R.id.tv_id);
            cardView = (CardView) itemView.findViewById(R.id.card);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            lineItem = (LinearLayout) itemView.findViewById(R.id.lineItem);
        }
    }


}